<template>
  <div class="login">
    <div class="login_box">
      <!--  顶部logo区域 -->
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!--  表单区域 -->

      <el-form
        ref="loginFormRef"
        class="login_form"
        :model="loginForm"
        :rules="loginFormRules"
      >
        <el-form-item prop="username">
          <i class="el-icon-s-custom"></i>
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <i class="el-icon-s-flag"></i>
          <el-input v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 12, message: '长度在 3 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      // 对整个表单进行校验 参数作为一个回调函数 返回的是一个布尔值
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('不能为空')
        }
        // 表单有值的话 发送axios请求
        // 把data解构出来 然后赋值给res
        // const { data: res } = await this.$http.post('/login', this.loginForm)
        // console.log(data)
        // console.log(res)
        const { data } = await this.$http.post('/login', this.loginForm)
        // console.log(data.meta)
        if (data.meta.status !== 200) {
          return this.$message.error(data.meta.msg)
        }
        // 登录成功
        // token进行认证
        // console.log(data)
        const token = data.data.token
        // console.log(token)
        localStorage.setItem('token', token)
        this.$message({
          message: '恭喜你，登录成功',
          type: 'success'
        })
        this.$router.push('/home')
      })
    },
    // 点击重置
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    }
  }
}
</script>

<style scoped   lang="less">
.login {
  /*  百分比相对于父元素说的   */
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    padding: 0 20px;
    .logo {
      position: absolute;
      left: 50%;
      margin-left: -76px;
      top: -76px;
      width: 152px;
      height: 152px;
      background-color: #fff;
      padding: 10px;
      border-radius: 50%;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
      img {
        width: 100%;
        height: 100%;
        background-color: #eee;
        border-radius: 50%;
      }
    }
  }

  .login_form {
    position: absolute;
    bottom: 20px;
    width: 410px;
    .btns {
      display: flex;
      justify-content: flex-end;
    }
    .el-icon-s-custom,
    .el-icon-s-flag {
      position: absolute;
      top: 13px;
      left: 12px;
      z-index: 10;
    }
  }
}
</style>
<style>
.el-input .el-input__inner {
  padding-left: 30px;
}
</style>
